package com.example.demo.common;

import org.springframework.stereotype.Component;
import org.springframework.web.servlet.HandlerInterceptor;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: ling
 * Date: 2023-01-18
 * Time: 17:11
 */
@Component
public class LoginInterceptor implements HandlerInterceptor {
    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        //判断登录业务
        HttpSession session = request.getSession(false);//会根据请求发送来的sessionId去服务器拿到会话session
        if(session!=null && session.getAttribute(SessionUtil.SESSION_USERINFO_KEY)!=null) {//根据key拿到value
            return true;
        }
        response.setStatus(401);
        return false;
    }
}
